/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.service;

import com.mycompany.delivery.dao.AccountDAO;
import com.mycompany.delivery.entities.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *
 * @author mhunek
 */
@Service(value = "accUserDetailsService")
public class AccountServiceImpl implements UserDetailsService {
  final static Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);
    
    @Autowired
    private AccountDAO accountDao;

    public AccountDAO getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDAO accountDao) {
        this.accountDao = accountDao;
    }
    
    @Override
    public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {
        UserDetails userDetails = null;
        Account account = accountDao.getByLogin(string);
        log.debug(string);
        
        if (account == null) {
            throw new UsernameNotFoundException("Account not found.");
        }
        
        userDetails = new AccountAdapter(account);
        return userDetails;
    }
    
    
}
